<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	
class Evaluation Extends MY_Controller
{
	var $current_user;
	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page('evaluation');
		$this->load->model(array('M_school_years','M_enrollees','M_users','M_news_and_events'));
		$this->current_user = $this->session->userdata('username').' - '.$this->session->userdata('userType');
		
	}
		
	public function index()
	{
		$this->view_data['system_message'] = $this->_msg();
	}
	
/*=========================================== Search =================================================*/
	function search(){
		
		if($this->uri->segment(3) == 'destroy_account' && $this->uri->segment(4) == 'success')
			$this->view_data['system_message'] = 'Successfully destroyed account.';
		else if($this->uri->segment(3) == 'destroy_account' && $this->uri->segment(4) == 'failed')
			$this->view_data['system_message'] = 'Cannot delete account, please try again.';
			
		if($this->uri->segment(3) == 'destroy_enrollee' && $this->uri->segment(4) == 'success')
			$this->view_data['system_message'] = 'Successfully destroyed enrollment.';
		else if($this->uri->segment(3) == 'destroy_enrollee' && $this->uri->segment(4) == 'failed')
			$this->view_data['system_message'] = 'Cannot delete enrolle, please try again.';
		
		if($this->input->get('submit')){
		$this->load->model('M_school_years');
		$sy = $this->M_school_years->get_set_school_year();
		
		foreach($sy as $syd):
			$syfrom = $syd->sy_from;
			$syto = $syd->sy_to;
		endforeach;
		
		
			$this->load->model('M_enrollees');
			$this->view_data['q_string'] = $this->input->get();
			$v = (object) $this->input->get();
			$this->view_data['enrollees'] = $this->M_enrollees->search_enrollee($v->lastname, $v->firstname, $v->idno, $syfrom, $syto);
			if(empty($this->view_data['enrollees']))
				$this->view_data['no_student'] = TRUE;

		}
	}
/*=========================================== End Search =================================================*/

/*=========================================== Enrollees =================================================*/
	function enrollee(){
		$this->load->model('M_student');
		$this->load->model('M_levels');
		$this->load->model('M_sections');
		$this->load->model('M_subjects');
		$this->load->model('M_enrolled_subjects');

		
		if($this->uri->segment(4) == 'update' && $this->uri->segment(5) == 'success')
			$this->view_data['system_message'] = 'Update success!';
		else if($this->uri->segment(4) == 'update' && $this->uri->segment('5') == 'profile_failed')
			$this->view_data['system_message'] = 'Student enrollment data updated';
		else if ($this->uri->segment(4) == 'update' && $this->uri->segment(5) == 'failed')
			$this->view_data['system_message'] = 'Profile update failed, please try again later';
		
				
		
		if($this->input->post('update')){
			
			if($this->form_validation->run('user_student_update') == FALSE){}
			else{
				$v = (object)$this->input->post();
								
				$enrollment_data['e_section_id'] = $v->section_id;
				$enrollment_data['e_grade_level'] = $v->level_id;
				$enrollment_data['e_sy_from'] = $v->sy_from;
				$enrollment_data['e_updated_at'] = NOW;
				$enrollment_data['e_enrollee_type'] = $v->student_type;
				
				$profile_data['lastname'] = $v->lastname;
				$profile_data['firstname'] = $v->firstname;
				$profile_data['middlename'] = $v->middlename;
				$profile_data['gender'] = $v->gender;
				$profile_data['civilstatus'] = $v->civilstatus;
				$profile_data['dob'] = date('Y-m-d',strtotime($v->dob));
				$profile_data['pob'] = $v->pob;
				$profile_data['age'] = $v->age;
				$profile_data['disability'] = $v->disability;
				$profile_data['nationality'] = $v->nationality;
				$profile_data['religion'] = $v->religion;
				$profile_data['contact_no'] = $v->contact_no;
				$profile_data['email'] = $v->email;
				$profile_data['present_address'] = $v->present_address;
				$profile_data['fathername'] = $v->fathername;
				$profile_data['father_occupation'] = $v->father_occupation;
				$profile_data['father_contact_no'] = $v->father_contact_no;
				$profile_data['mothername'] = $v->mothername;
				$profile_data['mother_occupation'] = $v->mother_occupation;
				$profile_data['mother_contact_no'] = $v->mother_contact_no;
				$profile_data['parent_address'] = $v->parent_address;
				$profile_data['guardian_name'] = $v->guardian_name;
				$profile_data['relationship'] = $v->relationship;
				$profile_data['guardian_contact_no'] = $v->guardian_contact_no;
				$profile_data['guardian_address'] = $v->guardian_address;
				$profile_data['last_school_name'] = $v->last_school_name;
				$profile_data['last_school_address'] = $v->last_school_address;
				$profile_data['last_school_level'] = $v->last_school_level;
				$profile_data['last_school_year'] = $v->last_school_year;
				
				if($v->section_id != ''){
					
					
					$subjects = $this->M_subjects->get_section_subject_ids($v->section_id);
					$subject_data = array();
					foreach($subjects as $subj_id){
						$data['enrollment_id'] = $v->enrollee_id;
						$data['subj_id'] = $subj_id->subj_id;
						$subject_data[] = $data;
					}
					
					$this->M_enrolled_subjects->assign_enrollee_subjects($subject_data);
				}
				
				
				$response = $this->M_enrollees->update_enrollee_profile_all($profile_data, $enrollment_data, $v->aprofile_id);
				if($response == 'success')
					redirect('evaluation/enrollee/'.$v->aprofile_id.'/update/success');
				else if($response == 'profile_failed')
					redirect('evaluation/enrollee/'.$v->aprofile_id.'/update/profile_failed');
				else
					redirect('evaluation/enrollee/'.$v->aprofile_id.'/update/failed');
				
			}
		}
			
		if($this->uri->segment(3) != ''){
			$this->view_data['student'] = $this->M_enrollees->get_enrollee_profile_all($this->uri->segment(3));
			$this->view_data['levels'] = $this->M_levels->get_all_levels();
			if(!empty($this->view_data['student'])){
				$this->view_data['subjects'] = $this->M_enrolled_subjects->get_enrollee_enrolled_subjects($this->view_data['student']->enrollee_id);
				$this->view_data['sections'] = $this->M_sections->get_avaialable_sections($this->view_data['student']->level_id);
				$this->view_data['section_id'] = $this->M_enrollees->get_enrollee_section_id($this->uri->segment(3));
			}
		}
	
			
			
			
	}
	
	function destroy_enrollment(){
		$query_string = '';
			foreach($this->input->get() as $key => $qq){
            	$query_string =	$query_string.$key.'='.$qq.'&';
            }

		
		if($this->M_enrollees->destroy_enrollee($this->uri->segment(3)))
			redirect('evaluation/search/destroy_enrollee/success/?'.$query_string);
		else
			redirect('evaluation/search/destroy_enrollee/failed/?'.$query_string);
			
		
	}
	
	function destroy_account(){
		$query_string = '';
			foreach($this->input->get() as $key => $qq){
            	$query_string =	$query_string.$key.'='.$qq.'&';
            }
			
		if($this->M_enrollees->destroy_account($this->uri->segment(3), $this->uri->segment(4)))
			redirect('evaluation/search/destroy_account/success/?'.$query_string);
		else
			redirect('evaluation/search/destroy_account/failed/?'.$query_string);
	}
/*=========================================== End Enrollees =================================================*/
	
	/********************
	Master List By Grade Level
	********************/
	public function master_list_by_grade_level()
	{
		$this->load->model('M_grade_level_sections');
		$this->load->model('M_levels');
		
		$sy = $this->M_school_years->get_set_school_year();
			
		foreach($sy as $syd):
			$syfrom = $syd->sy_from;
			$syto = $syd->sy_to;
		endforeach;
		
		$this->view_data['grade_level_data'] = $this->M_levels->get_all_levels();
		
		$this->view_data['grade_level_sections_data'] = $this->M_grade_level_sections->get_all_sections();
		
		$this->view_data['count_enrollees'] = $this->M_grade_level_sections->count_enrollees_per_section($syfrom,$syto);
	}
	
	public function view_enrollees($section_id,$level_id)
	{
		$this->load->model('M_sections');
		$this->load->model('M_levels');
		$this->load->model('M_grade_level_sections');
		$this->view_data['section'] = $this->M_sections->get_section($section_id);
		$this->view_data['level'] = $this->M_levels->get_grade_level($level_id);
		$this->view_data['enrollees'] = $this->M_grade_level_sections->get_enrollees($section_id,$level_id);
	}
	
	/*change user password*/
	function changepass($id='')
	{
		$old_pass 	= $this->input->post('old_password');
		$new_pass 	= $this->input->post('new_password');
		$c_new_pass = $this->input->post('c_new_password');
		$user_id = $this->input->post('id');
		$this->view_data['id'] = $id;
		if($this->form_validation->run('user_change_pass') == FALSE){
			
		}else
		{	
			if($this->M_users->verify_password($old_pass,$id)== TRUE){
				if($this->M_users->change_pass($new_pass,$id) == TRUE){
					$this->view_data['system_message'] = 'Password change successfull';
				}else{
					$this->view_data['system_message'] = 'Unable to update password';
				}
			}else{
				$this->view_data['system_message'] = 'Wrong Password Old Password';
			}
		}
	}
	
	/*Create News And Events*/
	function createnewsandevents()
	{ 

		if($this->form_validation->run('news_and_events') == FALSE)
		{

		}
		else
		{	
			$input['caption'] = $this->input->post('caption',TRUE);
			$input['announcement_date'] = date('Y-m-d',strtotime($this->input->post('announcement_date',TRUE)));
			$input['announcement'] = $this->input->post('announcement',TRUE);
			$input['posted_by'] = $this->session->userdata('userType');
		
			if($this->M_news_and_events->add_news_and_events($input)['status'] == 'true')
			{
				$this->view_data['system_message'] ='<div class="success">Data was Successfully Added</div>';					
			}
			elseif($this->M_news_and_events->add_news_and_events($input)['status'] == 'exist')
			{
				$this->view_data['system_message'] ='<div class="error">Data already Exists</div>';					
			}
			elseif($this->M_news_and_events->add_news_and_events($input)['status'] == 'false')
			{
				$this->view_data['system_message'] ='<div class="error">Error was encountered while processing request,Please Try Again Later</div>';					
			}
		}
	}
	
	/*Retrieve News And Events*/
	function newsandevents()
	{
		$this->view_data['date'] = $this->M_news_and_events->get_news_and_events();
		$this->view_data['total_rows'] = $this->M_news_and_events->count_news_and_events();
	
		$config['day_type'] = 'long'; 
		$config['show_next_prev'] = TRUE; 
		$config['next_prev_url'] = 'http://K12.gocloudsaas.info/evaluation/newsandevents/'; 
		
		$config['template'] = '
			{table_open}<table class="calendar">{/table_open}
			{heading_row_start}<tr>{/heading_row_start}
			{heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
			{heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
			{heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
			{heading_row_end}</tr>{/heading_row_end}
			{week_day_cell}<th class="day_header">{week_day}</th>{/week_day_cell}
			{cal_cell_content}<a class="with_content" href="{content}">{day}</a>{/cal_cell_content}
			{cal_cell_content_today}<a class="today_with_content" href="{content}">{day}</a>{/cal_cell_content_today}
			{cal_cell_no_content}<span class="day_listing">{day}</span>&nbsp;{/cal_cell_no_content}
			{cal_cell_no_content_today}<div class="today"><span class="day_listing">{day}</span></div>{/cal_cell_no_content_today}
		';  
		
		$this->load->library('calendar',$config);
	}
	
	/*Show News And Events*/
	function shownewsandevents($year,$month,$day)
	{
		$this->view_data['announcements'] = $this->M_news_and_events->get_news_and_events_today($year,$month,$day);
	}
}